import { AddressEditInfo } from "vant";
import { getAddressData } from "../../api";

export default function AddressAddEditHook() {
  const route = useRoute();
  const router = useRouter();
  let onSave = async (into: AddressEditInfo) => {
    // 点击保存后，如果有携带参数，那么先删除原来的
    if (route.query.id) {
      let res = await getAddressData({ status: "deleteAddress", userId: localStorage.getItem("token"), addressId: route.query.id});
      console.log(res);
    }
    let result = await getAddressData({
      status: "addAddress",
      userId: localStorage.getItem("token"),
      province: into.province,
      city: into.city,
      district: into.county,
      streetname: into.addressDetail,
      takename: into.name,
      postcode: into.areaCode,
      tel: into.tel
    })
    // 判断是否是默认地址，如果是就调取接口
    if (into.isDefault) {
      await getAddressData({ status: "defaultAddress", userId: localStorage.getItem("token"), addressId: result.data[0].address_id });
    }
    router.back();
  };
  return {
    onSave,
  }
}